<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生名称" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入学生名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="联系电话" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入联系电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>




      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['student:student:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['student:student:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['student:student:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['student:student:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="学号" align="center" prop="studentId" />
      <el-table-column label="状态" align="center" prop="userState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.student_state" :value="scope.row.userState" />
        </template>
      </el-table-column>
      <el-table-column label="学生名称" align="center" prop="userName" />
      <el-table-column label="学生性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.student_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="学生生日" align="center" prop="birthday" width="100" />
      <el-table-column label="联系电话" align="center" prop="phonenumber" />
      <el-table-column label="关系" align="center" prop="relationship">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.student_relationship" :value="scope.row.relationship" />
        </template>
      </el-table-column>
      <el-table-column label="就读学校" align="center" prop="school" />
      <el-table-column label="年级" align="center" prop="grade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.student_grade" :value="scope.row.grade" />
        </template>
      </el-table-column>
      <el-table-column label="住址" align="center" prop="address" />
      <el-table-column label="招生渠道" align="center" prop="enrollmentChannels">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.enrollment_channels" :value="scope.row.enrollmentChannels" />
        </template>
      </el-table-column>
      <el-table-column label="跟进人" align="center" prop="followUpPerson" />
      <!-- <el-table-column label="用户账号" align="center" prop="userAccount" /> -->
      <!-- <el-table-column label="密码" align="center" prop="password" /> -->
      <el-table-column label="帐号状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_oper_type" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="机构ID" align="center" prop="deptId" /> -->
      <!-- <el-table-column label="班级ID" align="center" prop="classId" /> -->
      <!-- <el-table-column label="头像地址" align="center" prop="avatar" /> -->
      <!-- <el-table-column label="最后登录IP" align="center" prop="loginIp" /> -->
      <!-- <el-table-column label="最后登录时间" align="center" prop="loginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="总课时" align="center" prop="totalClassHours" />
      <el-table-column label="剩余课时" align="center" prop="remainingClassHours" />
      <el-table-column label="累计学习课时" align="center" prop="accumulatedLearningHours" width="100" />
      <el-table-column label="标签" align="center" prop="label" />
      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['student:student:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['student:student:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />




    <el-drawer :title="title" :visible.sync="open" direction="rtl" size="50%" :show-close="false">
     <div v-show="type">
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="ml30">
              <div class="grid-content bg-purple-dark    text-size-23-w700  ">添加学员</div>

              <div class="grid-content bg-purple      text-size-20 mt20 text-gray   text-gray">请填写学员信息</div>

            </div>

          </el-col>
          <el-col :span="12">
            <div class="classflex-end mt20">

              <el-button type="primary"><i class="el-icon-upload " @click="piliang">批量添加</i></el-button>
              <el-button @click="cancel" style="width: 100px;margin-right: 100px;">取消</el-button>

            </div>

          </el-col>
        </el-row>

        <el-form class=" ml30 mr30" ref="form" :model="form" :rules="rules" label-width="80px" label-position="left">
          <el-divider style="width: 80%;"></el-divider>

          <el-form-item label="学员姓名" prop="userName">
            <el-input style="width: 450px;" v-model="form.userName" placeholder="请输入学员姓名" />
          </el-form-item>
          <el-form-item label="联系人" prop="relationship">
            <el-select style="width: 450px;" v-model="form.relationship" placeholder="请选择关系">
              <el-option v-for="dict in dict.type.student_relationship" :key="dict.value" :label="dict.label"
                :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系电话" prop="phonenumber">
            <el-input style="width: 450px;" v-model="form.phonenumber" placeholder="请输入联系电话" />
          </el-form-item>

          <el-divider style="width: 80%;">以下信息非必填</el-divider>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学号" prop="studentId">
                <el-input style="width: 350px;" v-model="form.studentId" placeholder="请输入学号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="跟进人" prop="followUpPerson">
                <el-input style="width: 350px;" v-model="form.followUpPerson" placeholder="请输入跟进人" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学生生日" prop="birthday">
                <el-date-picker style="width: 350px;" clearable v-model="form.birthday" type="date"
                  value-format="yyyy-MM-dd" placeholder="请选择学生生日">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="学生性别" prop="sex">
                <el-select style="width: 350px;" v-model="form.sex" placeholder="请选择学生性别">
                  <el-option v-for="dict in dict.type.student_sex" :key="dict.value" :label="dict.label"
                    :value="dict.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="就读学校" prop="school">
                <el-input style="width: 350px;" v-model="form.school" placeholder="请输入就读学校" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="年级" prop="grade">
                <el-select style="width: 350px;" v-model="form.grade" placeholder="请选择年级">
                  <el-optionstyle="width: 350px;" v-for="dict in dict.type.student_grade" :key="dict.value"
                    :label="dict.label" :value="dict.value">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="住址" prop="address">
                <el-input style="width: 350px;" v-model="form.address" placeholder="请输入住址" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="招生渠道" prop="enrollmentChannels">
                <el-select style="width: 350px;" v-model="form.enrollmentChannels" placeholder="请选择招生渠道">
                  <el-option v-for="dict in dict.type.enrollment_channels" :key="dict.value" :label="dict.label"
                    :value="dict.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="10">
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input style="width: 93%;" v-model="form.remark" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>

          </el-row>
          <el-divider></el-divider>
            <div>
              <el-button type="primary" @click="submitForm" style="width: 100%;">立即创建</el-button>

            </div>
          </el-form>
      </div>



      <div  v-show="!type">
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="ml30">
              <div class="grid-content bg-purple-dark    text-size-23-w700  ">添加学员</div>

              <div class="grid-content bg-purple     text-size-20 mt20 text-gray">请填写学员信息</div>

            </div>

          </el-col>
          <el-col :span="12">
            <div class="classflex-end mt20">

              <el-button @click="danyi" type="primary"><i class="el-icon-upload ">单一模式</i></el-button>
              <el-button @click="cancel" style="width: 100px;margin-right: 100px;">取消</el-button>

            </div>

          </el-col>
        </el-row>

        <el-form class=" ml30 mr30" ref="form" :model="form" :rules="rules" label-width="80px" label-position="left">
          <el-divider style="width: 80%;"></el-divider>



          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学员姓名" prop="userName">
                <el-input style="width: 350px;" v-model="form.userName" placeholder="请输入学员姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="phonenumber">
                <el-input style="width: 350px;" v-model="form.phonenumber" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学员姓名" prop="userName">
                <el-input style="width: 350px;" v-model="form.userName" placeholder="请输入学员姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="phonenumber">
                <el-input style="width: 350px;" v-model="form.phonenumber" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学员姓名" prop="userName">
                <el-input style="width: 350px;" v-model="form.userName" placeholder="请输入学员姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="phonenumber">
                <el-input style="width: 350px;" v-model="form.phonenumber" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学员姓名" prop="userName">
                <el-input style="width: 350px;" v-model="form.userName" placeholder="请输入学员姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="phonenumber">
                <el-input style="width: 350px;" v-model="form.phonenumber" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="学员姓名" prop="userName">
                <el-input style="width: 350px;" v-model="form.userName" placeholder="请输入学员姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="phonenumber">
                <el-input style="width: 350px;" v-model="form.phonenumber" placeholder="请输入联系电话" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider></el-divider>
            <div>
              <el-button type="primary" @click="submitForm" style="width: 100%;">立即创建</el-button>

            </div>
          </el-form>
      </div>






    </el-drawer>


  </div>
</template>

<script>
  import {
    listStudent,
    getStudent,
    delStudent,
    addStudent,
    updateStudent
  } from "@/api/student/student";

  export default {
    name: "Student",
    dicts: ['sys_oper_type', 'student_relationship', 'student_state', 'student_sex', 'enrollment_channels',
      'student_grade'
    ],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 学生信息表格数据
        studentList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
         type: false,

        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          studentId: null,
          userState: null,
          userName: null,
          sex: null,
          birthday: null,
          phonenumber: null,
          relationship: null,
          school: null,
          grade: null,
          address: null,
          enrollmentChannels: null,
          followUpPerson: null,
          userAccount: null,
          password: null,
          status: null,
          deptId: null,
          classId: null,
          avatar: null,
          loginIp: null,
          loginDate: null,
          totalClassHours: null,
          remainingClassHours: null,
          accumulatedLearningHours: null,
          label: null,
          remark2: null,
          remark3: null,
          remark4: null,
          remark5: null,
          remark6: null,
          remark7: null,
          remark8: null,
          remark9: null,
          remark10: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {

      this.getList();

    },
    methods: {
      piliang(){

          this.type = false;
      },
      danyi(){

          this.type = true;
      },
      /** 查询学生信息列表 */
      getList() {
        this.loading = true;
        this.queryParams.deptId = this.$store.state.user.deptId;
        listStudent(this.queryParams).then(response => {
          this.studentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          studentId: null,
          userState: null,
          userName: null,
          sex: null,
          birthday: null,
          phonenumber: null,
          relationship: null,
          school: null,
          grade: null,
          address: null,
          enrollmentChannels: null,
          followUpPerson: null,
          userAccount: null,
          password: null,
          status: null,
          deptId: null,
          classId: null,
          delFlag: null,
          avatar: null,
          loginIp: null,
          loginDate: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          totalClassHours: null,
          remainingClassHours: null,
          accumulatedLearningHours: null,
          label: null,
          remark: null,
          remark2: null,
          remark3: null,
          remark4: null,
          remark5: null,
          remark6: null,
          remark7: null,
          remark8: null,
          remark9: null,
          remark10: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        // this.title = "添加学生信息";
        this.form.deptId = this.$store.state.user.deptId;

      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getStudent(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改学生信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateStudent(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addStudent(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除学生信息编号为"' + ids + '"的数据项？').then(function() {
          return delStudent(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('student/student/export', {
          ...this.queryParams
        }, `student_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
